package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbCategory;
import com.guigu.qingcheng.service.TbCategoryService;
import com.guigu.qingcheng.utils.PageUtil;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
*
* @author Jiang
* @date 2020/07/27
*/
@Slf4j
@RestController
@RequestMapping("/category")
@Api(tags = "TbCategoryController")
public class TbCategoryController {
    @Resource
    private TbCategoryService tbCategoryService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {
        TbCategory tbCategory = tbCategoryService.findById(id);
        return R.ok().data("tbCategory",tbCategory);
    }

    @ApiOperation(value = "根据id获取信息 只查询name")
    @GetMapping("/detailname/{id}")
    public R detailname(@PathVariable("id") Integer id) {
        TbCategory tbCategory = tbCategoryService.findByIdName(id);
        return R.ok().data("name",tbCategory.getName());
    }


    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public R list() {
        List<TbCategory>  list = tbCategoryService.queryAll();
        return R.ok().data("list",list);
    }

    @ApiOperation(value = "获取信息（list不分页）根据ParentId查询")
    @GetMapping("/queryList/{parentId}")
    public R queryList(@PathVariable("parentId") Integer parentId) {
        List<TbCategory>  list = tbCategoryService.queryAllByparentId(parentId);
        return R.ok().data("list",list);
    }


    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages/{pageNum}/{pageSize}")
    public R listPages(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageUtil<TbCategory> pageUtils = tbCategoryService.findListAll(pageNum, pageSize);

        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public R pageQueryByAll(@RequestBody TbCategory tbCategory, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbCategory> pageUtils = tbCategoryService.pageQueryByAll(pageNum, pageSize, tbCategory);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public R add(@RequestBody TbCategory tbCategory) {

        boolean b = tbCategoryService.saveTbCategory(tbCategory);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R update(@RequestBody TbCategory tbCategory) {
        System.out.println(tbCategory.getIsMenu());
        boolean b = tbCategoryService.updateTbCategory(tbCategory);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        boolean b = tbCategoryService.deleteById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "获取所有分类")
    @GetMapping("/queryAllCategory")
    public R queryAllCategory() {
        List<Map> maps = tbCategoryService.queryAllCategory();
        return R.ok().data("maps",maps);
    }

    /**
     * 通过父类Id查询分页分类数据
     * @param parentId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "通过父类Id查询分页分类数据")
    @GetMapping("/getCategoryByParentId/{parentId}/{pageNo}/{pageSize}")
    public R getCategoryByParentId(@PathVariable("parentId")Integer parentId, @PathVariable("pageNo")Integer pageNo, @PathVariable("pageSize")Integer pageSize){
        PageUtil<TbCategory> page = tbCategoryService.getCategoryByParentId(parentId, pageNo, pageSize);
        return R.ok().message("查询成功").data("page", page);
    }

    /**
     * 查询分类(树状结构)
     * @return
     */
    @GetMapping("/findCategoryTree")
    public R findTree(){
        List<Map> categoryTree = tbCategoryService.findCategoryTree();
        return R.ok().message("查询成功").data("categoryTree", categoryTree);
    }

    // 今日推荐
    @GetMapping("today")
    public R Today(){
        List<Map> todayPush = tbCategoryService.TodayPush();
        return R.ok().message("查询成功").data("todayPush", todayPush);
    }

}
